﻿namespace HuaWei.CommClass
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Reflection;
    using System.Windows.Forms;

    public class EventManager : IDisposable
    {
        private EventHandlerList eventHandlerList = new EventHandlerList();
        private Hashtable hash = new Hashtable();

        public void AddEvent(object obj, string objName, string eventName, EventHandler eventhandler)
        {
            string str2;
            string key = objName + eventName;
            if (!this.hash.Contains(key))
            {
                this.hash.Add(key, new object());
            }
            object obj2 = this.hash[key];
            if (obj is BindingSource)
            {
                BindingSource source = obj as BindingSource;
                str2 = eventName;
                if ((str2 != null) && (str2 == "PositionChanged"))
                {
                    source.PositionChanged += eventhandler;
                }
            }
            else if (obj is DataTable)
            {
                DataTable table = obj as DataTable;
                str2 = eventName;
                if (str2 != null)
                {
                    if (!(str2 == "ColumnChanging"))
                    {
                        if (str2 == "ColumnChanged")
                        {
                            table.ColumnChanged += new DataColumnChangeEventHandler(eventhandler.Invoke);
                        }
                    }
                    else
                    {
                        table.ColumnChanging += new DataColumnChangeEventHandler(eventhandler.Invoke);
                    }
                }
            }
            else if (obj is Control)
            {
                Control control = obj as Control;
                str2 = eventName;
                if (str2 != null)
                {
                    if (!(str2 == "Click"))
                    {
                        if (str2 == "Enter")
                        {
                            control.Enter += eventhandler;
                        }
                    }
                    else
                    {
                        control.Click += eventhandler;
                    }
                }
            }
            this.eventHandlerList.AddHandler(obj2, eventhandler);
        }

        public static void ClearEvent(object obj, string eventName)
        {
            if ((obj != null) && !string.IsNullOrEmpty(eventName))
            {
                System.Type type = typeof(Control);
                EventHandlerList list = (EventHandlerList) type.GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance).GetValue(obj, null);
                FieldInfo field = typeof(Control).GetField("Event" + eventName, BindingFlags.NonPublic | BindingFlags.Static);
                Delegate delegate2 = list[field.GetValue(obj)];
                if (delegate2 != null)
                {
                    EventInfo info3 = type.GetEvent(eventName);
                    foreach (Delegate delegate3 in delegate2.GetInvocationList())
                    {
                        info3.RemoveEventHandler(obj, delegate3);
                    }
                }
            }
        }

        public void DelEvent(object obj, string objName, string eventName)
        {
            string str = objName + eventName;
            object key = this.hash[str];
            Delegate delegate2 = this.eventHandlerList[key];
            if (delegate2 != null)
            {
                foreach (Delegate delegate3 in delegate2.GetInvocationList())
                {
                    string str2;
                    if (obj is BindingSource)
                    {
                        BindingSource source = obj as BindingSource;
                        str2 = eventName;
                        if ((str2 != null) && (str2 == "PositionChanged"))
                        {
                            source.PositionChanged -= ((EventHandler) delegate3);
                        }
                    }
                    else if (obj is DataTable)
                    {
                        DataTable table = obj as DataTable;
                        str2 = eventName;
                        if (str2 != null)
                        {
                            if (!(str2 == "ColumnChanging"))
                            {
                                if (str2 == "ColumnChanged")
                                {
                                    table.ColumnChanged -= ((DataColumnChangeEventHandler) delegate3);
                                }
                            }
                            else
                            {
                                table.ColumnChanging -= ((DataColumnChangeEventHandler) delegate3);
                            }
                        }
                    }
                    else if (obj is Control)
                    {
                        Control control = obj as Control;
                        str2 = eventName;
                        if (str2 != null)
                        {
                            if (!(str2 == "Click"))
                            {
                                if (str2 == "Enter")
                                {
                                    control.Enter -= ((EventHandler) delegate3);
                                }
                            }
                            else
                            {
                                control.Click -= ((EventHandler) delegate3);
                            }
                        }
                    }
                }
                this.eventHandlerList.RemoveHandler(key, delegate2);
                this.hash.Remove(key);
            }
        }

        public void Dispose()
        {
            this.eventHandlerList.Dispose();
        }
    }
}

